<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 求 一个整数的n的阶乘
    // 1! + 2! + 3! + ... + n!

    /**
      求 阶乘 factorial
      参数： n
      返回值： 1-n之前阶乘的和
    */
    function factorial(n) {
      // 单独一个数的阶乘
      var sum = 1;
      // n个数阶乘的和
      var totalSum = 0;

      for (var i = 1; i <= n; i++) {
        sum *= i;
        totalSum += sum;
      }
      return totalSum;
    }

    var num = parseInt(prompt('请输入一个数', '3'));
    var sum = factorial(num);
    alert(num + '阶乘为' + sum);

  </script>
</body>

</html>